﻿<!--***********************************************************************************

   Extended WPF Toolkit

   Copyright (C) 2007-2013 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license 

   For more features, controls, and fast professional support,
   pick up the Plus Edition at http://xceed.com/wpf_toolkit

   Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids

  **********************************************************************************-->

<FrameworkElement x:Class="Xceed.Wpf.DataGrid.DefaultCellEditorSelector"
                  x:ClassModifier="internal"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:xctk="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
                  xmlns:local="clr-namespace:Xceed.Wpf.DataGrid"
                  xmlns:conv="clr-namespace:Xceed.Wpf.DataGrid.Converters"
                  xmlns:markup="clr-namespace:Xceed.Wpf.DataGrid.Markup"
                  xmlns:s="clr-namespace:System;assembly=mscorlib">
   <FrameworkElement.Resources>

      <conv:NullToBooleanConverter x:Key="nullToBooleanConverter" />

      <ControlTemplate x:Key="scrollViewerTemplate"
                       TargetType="{x:Type ScrollViewer}">
         <ScrollContentPresenter CanContentScroll="{TemplateBinding CanContentScroll}" />
      </ControlTemplate>

      <ControlTemplate x:Key="textBoxTemplate"
                       TargetType="{x:Type TextBox}">
         <ScrollViewer  x:Name="PART_ContentHost"
                        Template="{StaticResource scrollViewerTemplate}" />
      </ControlTemplate>

      <DataTemplate x:Key="textBoxEditorTemplate">
         <xctk:AutoSelectTextBox Text="{markup:CellEditorBinding}"
                                    local:DataCell.IsCellFocusScope="True"
                                    VerticalAlignment="Stretch"
                                    Template="{StaticResource textBoxTemplate}"
                                    Margin="-2,0,-2,0"
                                    Background="Transparent"
                                    Foreground="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(TextElement.Foreground)}"
                                    AutoSelectBehavior="OnFocus"
                                    AutoMoveFocus="True"
                                    AutomationProperties.AutomationId="TextBoxEditor" />
      </DataTemplate>

      <!--Also used by CellEditor.TextBoxEditor-->
      <local:CellEditor x:Key="System.String"
                        EditTemplate="{StaticResource textBoxEditorTemplate}">
         <local:CellEditor.ActivationGestures>
            <local:TextInputActivationGesture />
         </local:CellEditor.ActivationGestures>
      </local:CellEditor>

      <local:CellEditor x:Key="System.Int16">
         <local:CellEditor.EditTemplate>
            <DataTemplate DataType="{x:Type s:Int16}">
                    <local:ValidateInputWrapper>
                        <xctk:ShortUpDown Name="PART_CellEditorBoundControl"
                                          Value="{markup:CellEditorBinding}"
                                          local:DataCell.IsCellFocusScope="True"
                                          AllowSpin="False"
                                          ShowButtonSpinner="False"
                                          AutoMoveFocus="True"
                                          VerticalAlignment="Stretch"
                                          Margin="-2,0,-2,0"
                                          Background="Transparent"
                                          Foreground="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(TextElement.Foreground)}"
                                          AutomationProperties.AutomationId="NumericTextBoxEditor" />
                    </local:ValidateInputWrapper>
                </DataTemplate>
         </local:CellEditor.EditTemplate>
         <local:CellEditor.ActivationGestures>
            <local:TextInputActivationGesture />
         </local:CellEditor.ActivationGestures>
      </local:CellEditor>

      <local:CellEditor x:Key="System.Int32">
         <local:CellEditor.EditTemplate>
            <DataTemplate DataType="{x:Type s:Int32}">
                <local:ValidateInputWrapper>
                    <xctk:IntegerUpDown Name="PART_CellEditorBoundControl"
                                        Value="{markup:CellEditorBinding}"
                                        local:DataCell.IsCellFocusScope="True"
                                        AllowSpin="False"
                                        ShowButtonSpinner="False"
                                        AutoMoveFocus="True"
                                        VerticalAlignment="Stretch"
                                        Margin="-2,0,-2,0"
                                        Background="Transparent"
                                        Foreground="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(TextElement.Foreground)}"
                                        AutomationProperties.AutomationId="NumericTextBoxEditor" />
                </local:ValidateInputWrapper>
            </DataTemplate>
         </local:CellEditor.EditTemplate>

         <local:CellEditor.ActivationGestures>
            <local:TextInputActivationGesture />
         </local:CellEditor.ActivationGestures>
      </local:CellEditor>

      <local:CellEditor x:Key="System.Int64">
         <local:CellEditor.EditTemplate>
            <DataTemplate DataType="{x:Type s:Int64}">
                <local:ValidateInputWrapper>
                    <xctk:LongUpDown Name="PART_CellEditorBoundControl"
                                     Value="{markup:CellEditorBinding}"
                                     local:DataCell.IsCellFocusScope="True"
                                     AllowSpin="False"
                                     ShowButtonSpinner="False"
                                     AutoMoveFocus="True"
                                     VerticalAlignment="Stretch"
                                     Margin="-2,0,-2,0"
                                     Background="Transparent"
                                     Foreground="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(TextElement.Foreground)}"
                                     AutomationProperties.AutomationId="NumericTextBoxEditor" />
                </local:ValidateInputWrapper>
            </DataTemplate>
         </local:CellEditor.EditTemplate>
         <local:CellEditor.ActivationGestures>
            <local:TextInputActivationGesture />
         </local:CellEditor.ActivationGestures>
      </local:CellEditor>

      <local:CellEditor x:Key="System.Single">
         <local:CellEditor.EditTemplate>
            <DataTemplate DataType="{x:Type s:Single}">
                <local:ValidateInputWrapper>
                    <xctk:SingleUpDown Name="PART_CellEditorBoundControl"
                                    Value="{markup:CellEditorBinding}"
                                    local:DataCell.IsCellFocusScope="True"
                                    AllowSpin="False"
                                    ShowButtonSpinner="False"
                                    AutoMoveFocus="True"
                                    VerticalAlignment="Stretch"
                                    Margin="-2,0,-2,0"
                                    Background="Transparent"
                                    Foreground="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(TextElement.Foreground)}"
                                    AutomationProperties.AutomationId="NumericTextBoxEditor" />
                </local:ValidateInputWrapper>
            </DataTemplate>
         </local:CellEditor.EditTemplate>
         <local:CellEditor.ActivationGestures>
            <local:TextInputActivationGesture />
         </local:CellEditor.ActivationGestures>
      </local:CellEditor>

      <local:CellEditor x:Key="System.Double">
         <local:CellEditor.EditTemplate>
            <DataTemplate DataType="{x:Type s:Double}">
                <local:ValidateInputWrapper>
                    <xctk:DoubleUpDown Name="PART_CellEditorBoundControl"
                                       Value="{markup:CellEditorBinding}"
                                       local:DataCell.IsCellFocusScope="True"
                                       AllowSpin="False"
                                       ShowButtonSpinner="False"
                                       AutoMoveFocus="True"
                                       VerticalAlignment="Stretch"
                                       Margin="-2,0,-2,0"
                                       Background="Transparent"
                                       Foreground="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(TextElement.Foreground)}"
                                       AutomationProperties.AutomationId="NumericTextBoxEditor" />
                </local:ValidateInputWrapper>
            </DataTemplate>
         </local:CellEditor.EditTemplate>
         <local:CellEditor.ActivationGestures>
            <local:TextInputActivationGesture />
         </local:CellEditor.ActivationGestures>
      </local:CellEditor>

      <local:CellEditor x:Key="System.Decimal">
         <local:CellEditor.EditTemplate>
            <DataTemplate DataType="{x:Type s:Decimal}">
                <local:ValidateInputWrapper>
                    <xctk:DecimalUpDown Name="PART_CellEditorBoundControl"
                                        Value="{markup:CellEditorBinding}"
                                        local:DataCell.IsCellFocusScope="True"
                                        AllowSpin="False"
                                        ShowButtonSpinner="False"
                                        AutoMoveFocus="True"
                                        VerticalAlignment="Stretch"
                                        Margin="-2,0,-2,0"
                                        Background="Transparent"
                                        Foreground="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(TextElement.Foreground)}"
                                        AutomationProperties.AutomationId="NumericTextBoxEditor" />
                </local:ValidateInputWrapper>
            </DataTemplate>
         </local:CellEditor.EditTemplate>
         <local:CellEditor.ActivationGestures>
            <local:TextInputActivationGesture />
         </local:CellEditor.ActivationGestures>
      </local:CellEditor>

      <!--Also used by CellEditor.CheckBoxEditor-->
      <local:CellEditor x:Key="System.Boolean">
         <local:CellEditor.EditTemplate>
            <DataTemplate DataType="{x:Type s:Boolean}">
               <local:DataGridCheckBox IsChecked="{markup:CellEditorBinding}"
                               local:DataCell.IsCellFocusScope="True"
                               VerticalAlignment="Stretch"
                               Foreground="{Binding RelativeSource={RelativeSource TemplatedParent},  Path=(TextElement.Foreground)}"
                               AutomationProperties.AutomationId="CheckBoxEditor" />
            </DataTemplate>
         </local:CellEditor.EditTemplate>
         <local:CellEditor.ActivationGestures>
            <local:KeyActivationGesture Key="Space" />
         </local:CellEditor.ActivationGestures>
      </local:CellEditor>

      <local:CellEditor x:Key="System.Xml.XmlElement"
                        EditTemplate="{StaticResource textBoxEditorTemplate}">

         <local:CellEditor.ActivationGestures>
            <local:TextInputActivationGesture />
         </local:CellEditor.ActivationGestures>
      </local:CellEditor>

      <!--Also used by CellEditor.DatePickerEditor-->
      <local:CellEditor x:Key="System.DateTime">
         <local:CellEditor.EditTemplate>
            <DataTemplate DataType="{x:Type s:DateTime}">
               <local:ValidateInputWrapper>
                  <local:DataGridDatePicker SelectedDate="{markup:CellEditorBinding}"
                                            x:Name="PART_CellEditorBoundControl"
                                            VerticalAlignment="Top"
                                            Foreground="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(TextElement.Foreground)}"
                                            AutomationProperties.AutomationId="DatePickerEditor" />
               </local:ValidateInputWrapper>
            </DataTemplate>
         </local:CellEditor.EditTemplate>
         <local:CellEditor.ActivationGestures>
            <local:KeyActivationGesture SystemKey="Down"
                                        Modifiers="Alt" />
            <local:KeyActivationGesture SystemKey="Up"
                                        Modifiers="Alt" />
            <local:TextInputActivationGesture />
         </local:CellEditor.ActivationGestures>
      </local:CellEditor>

      <!-- ForeignKey CellEditor -->
      <local:CellEditor x:Key="foreignKeyCellEditor">
         <local:CellEditor.EditTemplate>
            <DataTemplate>
               <ComboBox x:Name="fkComboBox"
                         local:Cell.IsCellFocusScope="True"
                         BorderThickness="0"
                         ItemTemplate="{Binding RelativeSource={RelativeSource Self}, Path=(local:Cell.CellEditorContext).ParentColumn.CellContentTemplate, Mode=OneWay}"
                         ItemContainerStyle="{Binding RelativeSource={RelativeSource Self}, Path=(local:Cell.CellEditorContext).ForeignKeyConfiguration.ItemContainerStyle, Mode=OneWay}"
                         ItemsSource="{Binding RelativeSource={RelativeSource Self}, Path=(local:Cell.CellEditorContext).ForeignKeyConfiguration.ItemsSource, Mode=OneWay}"
                         SelectedValuePath="{Binding RelativeSource={RelativeSource Self}, Path=(local:Cell.CellEditorContext).ForeignKeyConfiguration.ValuePath, Mode=OneWay}"
                         SelectedValue="{markup:CellEditorBinding}"
                         AutomationProperties.AutomationId="ForeignKeyCellEditor" />

               <!-- Only affect Selector if Template is null and DisplayMemberPath is null or empty -->
               <DataTemplate.Triggers>
                  <MultiDataTrigger>
                     <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(local:Cell.CellEditorContext).ParentColumn.CellContentTemplate, Converter={StaticResource nullToBooleanConverter}, Mode=OneWay}"
                                   Value="True" />
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(local:Cell.CellEditorContext).ParentColumn.CellContentTemplateSelector, Converter={StaticResource nullToBooleanConverter}, Mode=OneWay}"
                                   Value="False" />
                     </MultiDataTrigger.Conditions>
                     <MultiDataTrigger.Setters>
                        <Setter TargetName="fkComboBox"
                                Property="ItemTemplateSelector"
                                Value="{Binding RelativeSource={RelativeSource Self}, Path=(local:Cell.CellEditorContext).ParentColumn.CellContentTemplateSelector, Mode=OneWay}" />
                     </MultiDataTrigger.Setters>
                  </MultiDataTrigger>

                  <MultiDataTrigger>
                     <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(local:Cell.CellEditorContext).ParentColumn.CellContentTemplate, Converter={StaticResource nullToBooleanConverter}, Mode=OneWay}"
                                   Value="True" />
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(local:Cell.CellEditorContext).ParentColumn.CellContentTemplateSelector, Converter={StaticResource nullToBooleanConverter}, Mode=OneWay}"
                                   Value="True" />
                     </MultiDataTrigger.Conditions>
                     <MultiDataTrigger.Setters>
                        <Setter TargetName="fkComboBox"
                                Property="DisplayMemberPath"
                                Value="{Binding RelativeSource={RelativeSource Self}, Path=(local:Cell.CellEditorContext).ForeignKeyConfiguration.DisplayMemberPath, Mode=OneWay}" />
                     </MultiDataTrigger.Setters>
                  </MultiDataTrigger>

                  <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(local:Cell.CellEditorContext).ForeignKeyConfiguration.ItemContainerStyle, Converter={StaticResource nullToBooleanConverter}, Mode=OneWay}"
                               Value="True">
                     <Setter TargetName="fkComboBox"
                             Property="ItemContainerStyleSelector"
                             Value="{Binding RelativeSource={RelativeSource Self}, Path=(local:Cell.CellEditorContext).ForeignKeyConfiguration.ItemContainerStyleSelector, Mode=OneWay}" />
                  </DataTrigger>
               </DataTemplate.Triggers>
            </DataTemplate>
         </local:CellEditor.EditTemplate>
         <local:CellEditor.ActivationGestures>
            <local:KeyActivationGesture SystemKey="Down"
                                        Modifiers="Alt" />

            <local:KeyActivationGesture SystemKey="Up"
                                        Modifiers="Alt" />

            <local:KeyActivationGesture Key="F4" />

            <local:KeyActivationGesture Key="Space" />
         </local:CellEditor.ActivationGestures>
      </local:CellEditor>

   </FrameworkElement.Resources>

</FrameworkElement>
